package com.movisol.messengercommunicator.asynctasks;

import android.os.AsyncTask;
import android.widget.Toast;

import com.movisol.messengercommunicator.controllers.CommunicatorController;
import com.movisol.messengercommunicator.helper.MessengerCommunicatorHelper;
import com.movisol.messengerprofileservice.controllers.ProfileController;
import com.movisol.messengerprofileservice.helpers.MessengerProfileHelper;
import com.movisol.messengerprofileservice.interfaces.errorcodes.SaveUserProfileErrorCodes;
import com.movisol.tools.HelperUtils;

public class AsyncGetConversationsList extends AsyncTask<Void, Void, Void> {

	private ProfileController		pc	= ProfileController.getInstance();
	private CommunicatorController	cc	= CommunicatorController.getInstance();

	@Override
	protected Void doInBackground(Void... params) {
		String userId = pc.getUserProfile().getUserId();
		String signature = MessengerProfileHelper.generateSignature(userId
				+ HelperUtils.getConfigParam("ChatSecretPhrase", cc.getContext()));
		String result = cc.getConversationList(userId, signature);
		cc.setCommunicatorUri4Conversations(MessengerCommunicatorHelper.parseXMLConversationToObject(result));
		// cachear communicatorUri4Conversations
		if (cc.getCommunicatorUri4Conversations() != null)
			cc.writeConversationUriToFile(pc.getConversationUriCacheFileName());
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (cc.getConversationCache() != null)
			new AsyncLoadConversationsList().execute();
		else
		{
			HelperUtils.LogE(getClass(), "cc.getConversationCache() is NULL");
			HelperUtils.showToastMessage(cc.getContext(),
					SaveUserProfileErrorCodes.getErrorByCode(SaveUserProfileErrorCodes.INTERNAL_ERROR),
					Toast.LENGTH_LONG);
		}
	}
}
